/**
 *******************************************************************************
 * @FileName  : beep.c
 * @Author    : GaoQiu
 * @CreateDate: 2024年1月8日
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#include "bsp/bsp.h"
#include "utils/sys_time.h"

#define PWM_ID    PWM1
#define PWM_CHN   0

uint16_t lowTone[]  = {262,  294,  330,  349,  392,  440,  494};
uint16_t MidTone[]  = {523,  587,  659,  698,  784,  880,  988};
uint16_t HighTone[] = {1046, 1175, 1318, 1397, 1568, 1760, 1967};

#define L1   262
#define L2   294
#define L3   330
#define L4   349
#define L5   392
#define L6   440
#define L7   494

#define M1   523
#define M2   587
#define M3   659
#define M4   698
#define M5   784
#define M6   880
#define M7   988

#define H1   1046
#define H2   1175
#define H3   1318
#define H4   1397
#define H5   1568
#define H6   1760
#define H7   1967

#define Z0   0


int solitary_brave[] =
{
#if 0
	M6,50,M7,50,H1,50,H2,50,M7,50,H1,50,H1,100,Z0,10,	//爱你孤身走暗巷
	H1,50,M7,50,H1,50,H2,50,M7,50,H1,50,H1,100,Z0,10, 	//爱你不跪的模样
	H1,50,H2,50,H3,50,H2,50,H3,50,H2,50,H3,100,H3,50,H3,50,H2,50,H3,100,H5,100,H3,100,Z0,10, //爱你对峙过绝望不肯哭一场
	M6,50,M7,50,H1,50,H2,50,M7,50,H1,50,H1,100,Z0,10,
	H1,50,M7,50,H1,50,H2,50,M7,50,H1,50,H1,100,Z0,10,
#else

	//j=65 1=B 4/4
	M6,231,M7,231, H1,231,H2,231,M7,231,H1,231,H1,462,
	H1,231,M7,231, H1,231,H2,231,M7,231,H1,231,H1,462,
	H1,231,M2,231, H3,231,H2,231,H3,231,H2,231,H3,462,  H3,231,H2,231, H3,500, H5,500, H3,462,
	M6,231,M7,231, H1,231,H2,231,M7,231,H1,231,H1,462,
	H1,231,M7,231, H1,231,H2,231,M7,231,H1,231,H1,462,
	H1,231,H2,231, H3,231,H2,231,H3,231,H2,231,H3,462,
	H3,231,H2,231, H3,500,H5,500,H3,500,H5,500,H3,693,
	H5,231,H3,693, H5,231,H3,231,H5,231,H6,231,H3,231, H5,500,H5,500,H3,693,H5,231,H3,693,H5,231,H3,231,H5,231,H6,231,H3,231,H5,462,H5,231,H5,231,
	Z0,0,
#endif
};

void Buzzer_Start(uint32_t freqHz)
{
#ifdef IP_108
	uint16_t period = 1000000/freqHz;

	PWM_SetCNR(PWM_ID, PWM_CHN, period);
	PWM_SetCMR(PWM_ID, PWM_CHN, period/2);

	PWM_Start(PWM_ID, BIT(PWM_CHN));
#endif
}

void Buzzer_Stop(void)
{
#ifdef IP_108
	PWM_Stop(PWM_ID, BIT(PWM_CHN));
#endif
}

void Buzzer_PlayMusic(void)
{
#ifdef IP_108
	GPIO_Init(GPIO_P24, GPIO_Mode_PP_Out);
	GPIO_SetAFunc(GPIO_P24, GPIO_AF_4);

	CLK_APB1PeriphClockCmd(CLK_APB1Periph_PWM1_CH01|CLK_APB1Periph_PWM1_EN, ENABLE);

	PWM_SetPrescaler(PWM_ID, PWM_CHN, 48-1);
	PWM_SetDivider(PWM_ID, PWM_CHN, PWM_CLK_DIV_1);
	PWM_SetAlignedType(PWM_ID, BIT(PWM_CHN), PWM_EDGE_ALIGNED);
	PWM_EnableIndependentMode(PWM_ID);
	PWM_EnableOutput(PWM_ID, BIT(PWM_CHN));
#endif

#if 0
	for(int i=0; i<7; i++){
		Buzzer_Start(lowTone[i]);
		sleep_us(200*1000);
		Buzzer_Stop();

		sleep_us(50*1000);
	}

	for(int i=0; i<7; i++){
		Buzzer_Start(MidTone[i]);
		sleep_us(200*1000);
		Buzzer_Stop();

		sleep_us(50*1000);
	}

	for(int i=0; i<7; i++){
		Buzzer_Start(HighTone[i]);
		sleep_us(200*1000);
		Buzzer_Stop();

		sleep_us(50*1000);
	}
#else
	//bbCb.sleepClockSrc = 2;
	//BB_Calibrate32kRC(32000);

	int length = sizeof(solitary_brave)/sizeof(solitary_brave[0]);
	for(uint8_t i=0;i<(length/2);i++)
	{
		Buzzer_Start(solitary_brave[i*2]);
		//sleep_us(5000*solitary_brave[i*2+1]);
		//delay_ms(solitary_brave[i*2+1] * 5);
		delay_ms(solitary_brave[i*2+1]);
		Buzzer_Stop();
	}
#endif

	//while(1);
}
